package com.droidbank;

import java.util.Date;

import com.droidbank.model.Account;
import com.droidbank.model.DataModel;
import com.droidbank.model.Expense;
import com.droidbank.util.FormatDecimal;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class TransferActivity extends Activity{

	public void onCreate(Bundle savedInstanceState) 
	{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.transfer_layout);
        
        final Spinner spinFrom = (Spinner) findViewById(R.id.toSpinner);
        final ArrayAdapter<String> spinAdapFrom = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
        spinAdapFrom.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinFrom.setAdapter(spinAdapFrom);
        
        final Spinner spinTo = (Spinner) findViewById(R.id.fromSpinner);
        final ArrayAdapter<String> spinAdapTo = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
        spinAdapTo.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinTo.setAdapter(spinAdapTo);
        
        final DataModel dm = (DataModel)this.getApplication();
        
        for(Account a: dm.getAccounts()){
        	spinAdapFrom.add(a.getName());
        	spinAdapTo.add(a.getName());
        }
        
        
        final EditText amount = (EditText) findViewById(R.id.transferAmountEdit);
        final Button add = (Button) findViewById(R.id.transferButton);
        add.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) 
            {
            	if(amount.getText().toString().isEmpty()){return;}
            	
            	double amountd = Double.valueOf(amount.getText().toString());
            	
            	if(spinFrom.getSelectedItemPosition() == spinTo.getSelectedItemPosition()){
            		Toast t = Toast.makeText(TransferActivity.this,"Cannot Transfer to Same Account", 
                			Toast.LENGTH_SHORT);
            		t.setGravity(Gravity.CENTER, 0, 0);
            		t.show();
            		
            	}
            	else if(amountd <= 0)
            	{
            		Toast t = Toast.makeText(TransferActivity.this,"Amount needs to be > 0", 
                			Toast.LENGTH_SHORT);
            		t.setGravity(Gravity.CENTER, 0, 0);
            		t.show();
            	}
            	else
            	{
            		Account aFrom = dm.getAccounts().get(spinFrom.getSelectedItemPosition());
            		aFrom.addExpense(new Expense(new Date().getTime(), Expense.TRANSFER, "Transfer to " + spinTo.getSelectedItem(), "",
            				FormatDecimal.format( 2, -amountd)));
            		
            		Account aTo = dm.getAccounts().get(spinTo.getSelectedItemPosition());
            		aTo.addExpense(new Expense(new Date().getTime(), Expense.TRANSFER, "Transfer from " + spinFrom.getSelectedItem(), "",
            				FormatDecimal.format( 2, amountd)));
            		
            		Toast t = Toast.makeText(TransferActivity.this,"Transfer Completed", 
                			Toast.LENGTH_SHORT);
            		t.setGravity(Gravity.CENTER, 0, 0);
            		t.show();
            		
            		DataModel.isDirty = true;
            	}
            		
            	
            }
        });
	}
}
